#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>
#include <sdkhooks>



ConVar C_WitchDamage;

float F_WitchDamage;

#define CVAR_FLAGS			FCVAR_NOTIFY

#define PLUGIN_NAME                   "l4d2_Melee_damage_charger"    //这里写插件名字
#define PLUGIN_AUTHOR                 "豆瓣酱な"                         //作者
#define PLUGIN_DESCRIPTION            "修改近战武器对charger的伤害"   //介绍
#define PLUGIN_VERSION                "1.0"
#define PLUGIN_URL                    "https://gitee.com/sweet-and-sour-bombs"  //网站

public Plugin myinfo =
{
    name        = PLUGIN_NAME,
    author      = PLUGIN_AUTHOR,
    description = PLUGIN_DESCRIPTION,
    version     = PLUGIN_VERSION,
    url         = PLUGIN_URL
}

public void OnPluginStart()
{


	C_WitchDamage = CreateConVar("l4d2_charger_damage_modify", "50.0", "近战武器对Charger的伤害值", CVAR_FLAGS);
	
	CreateConVar("l4d2_Melee_damage_charger_version", PLUGIN_VERSION, "插件版本", FCVAR_NOTIFY);	
	
	C_WitchDamage.AddChangeHook(convar_changed);

	//AutoExecConfig(true, "l4d2_Melee_damage_charger");
}

void convar_changed(ConVar convar, const char[] oldValue, const char[] newValue)
{
	get_cvars();
}

void get_cvars()
{
	F_WitchDamage = C_WitchDamage.FloatValue;
}

public void OnConfigsExecuted()
{
	get_cvars();
}

public void OnClientPutInServer(int client)
{
	SDKHook(client, SDKHook_OnTakeDamage, OnTakeDamage);
}

public Action OnTakeDamage(int victim, int &attacker, int &inflictor, float &damage, int &damagetype, int &weapon, float damageForce[3], float damagePosition[3], int damagecustom)
{
	if (IsMelee(inflictor) && IsValidClient(attacker))
	{


		
		if (IsCharger(victim))
			{
				damage = F_WitchDamage;
				return Plugin_Changed;
			}
		
	}
	return Plugin_Continue;
}



//玩家团队判定
bool IsValidClient(int client)
{
	return client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == 2;
}



//近战判定
bool IsMelee(int inflictor)
{
	if(inflictor > MaxClients)
	{
		static char classname[13];
		GetEdictClassname(inflictor, classname, sizeof(classname));
		return strcmp(classname, "weapon_melee") == 0;
	}
	return false;
}

//Charger判定
bool IsCharger(int victim)
{
	return victim > 0 && victim <= MaxClients && IsClientInGame(victim) && GetClientTeam(victim) == 3 && GetEntProp(victim, Prop_Send, "m_zombieClass") == 6;
}
